package com.keyinong.fragment;

import java.util.ArrayList;

import org.apache.http.Header;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshBase.Mode;
import com.handmark.pulltorefresh.library.PullToRefreshBase.OnRefreshListener2;
import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.keyinong.qudan.R;
import com.keyinong.qudan.ScanActivity;
import com.keyinong.qudan.dapter.OrderAdapter;
import com.keyinong.qudan.entity.OrderEntity;
import com.keyinong.qudan.tools.HttpTools;
import com.keyinong.qudan.tools.NetWorkUtil;
import com.keyinong.qudan.view.OrderDetailActivity;
import com.loopj.android.http.JsonHttpResponseHandler;

public class YijiedanFragment extends Fragment{
	
	/**上下拉刷新*/
	private PullToRefreshListView list_order;
	HttpTools httpTools = null;
	private ArrayList<OrderEntity> orderlist;
	
	int page = 10;
	int offset = 0;
	private String token;
	private String state;
	private TextView tv_dingdan;
	
	/**扫一扫*/
	private ImageButton btn_saoyisao;
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		return inflater.inflate(R.layout.fragment_yijiedan, null);
		
	}
	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);
		orderlist = new ArrayList<OrderEntity>();
		list_order=(PullToRefreshListView)getView().findViewById(R.id.list_order);
		list_order.setMode(Mode.BOTH);
		list_order.setOnRefreshListener(onrefresh);
		list_order.setOnItemClickListener(itemclicke);
		tv_dingdan=(TextView)getView().findViewById(R.id.tv_dingdan);
		myOrder();
		btn_saoyisao=(ImageButton)getView().findViewById(R.id.btn_saoyisao);
		btn_saoyisao.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				Intent intent = new Intent();
				intent.setClass(getActivity(), ScanActivity.class);
				startActivity(intent);
			}
		});
	}
	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
//		myOrder();
	}
	/** 上下拉刷新 */
	private OnRefreshListener2<ListView> onrefresh = new OnRefreshListener2<ListView>() {

		@Override
		public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
			offset = 0;
			orderlist.clear();
			if (httpTools == null) {
				httpTools = new HttpTools(getActivity());
			}
			httpTools.order(token, page + "", offset + "", "2","1",new JsonMyOrder());
		}

		@Override
		public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
			offset += page;
			if (httpTools == null) {
				httpTools = new HttpTools(getActivity());
			}
			httpTools.order(token, page + "", offset + "","2","1", new JsonMyOrder());
			// setSection();
			// adapter.notifyDataSetChanged();
		}

	};
	
	private OnItemClickListener itemclicke = new OnItemClickListener() {

		@Override
		public void onItemClick(AdapterView<?> parent, View view,final int position,
				long id) {
			Intent intent = new Intent();
			intent.putExtra("id", orderlist.get(position-1).getId());
			intent.setClass(getActivity(), OrderDetailActivity.class);
//			Log.i("zq", "test-------------------");
			startActivity(intent);
		}
	};
	
	
	/**待取餐订单*/
	private void myOrder() {
		if(NetWorkUtil.isNetwork(getActivity())==false)
		{
			Toast.makeText(getActivity(), "当前网络不可用", Toast.LENGTH_SHORT).show();
			return;
		}
		SharedPreferences sp = getActivity().getSharedPreferences("spReg",
				Context.MODE_PRIVATE);
		token = sp.getString("token", "");
//		TTs();
		if (httpTools == null) {
			httpTools = new HttpTools(getActivity());
		}
		httpTools.order(token, page + "", offset + "", "2","1",new JsonMyOrder());
	}
	
	private class JsonMyOrder extends JsonHttpResponseHandler {
		@Override
		public void onSuccess(int statusCode, Header[] headers,
				JSONObject response) {
			super.onSuccess(statusCode, headers, response);
			try {
				boolean code = response.getBoolean("code");
				if (code == true) {
					JSONObject object = response.getJSONObject("data");
//					String isnew = object.getString("isnew");
					JSONArray array = object.getJSONArray("orders");
					for (int i = 0; i < array.length(); i++) {
						JSONObject object2 = array.getJSONObject(i);
						String id = object2.getString("id");
						String dayno = object2.getString("dayno");
						String receive_time = object2.getString("receive_time");
						state = object2.getString("state");
						String name = object2.getString("name");
						String phone = object2.getString("phone");
						String address = object2.getString("address");
						String recmode = object2.getString("recmode");
						String paymodeid = object2.getString("paymodeid");
						if(state.equals("null")){
							Toast.makeText(getActivity(), "没有订单", Toast.LENGTH_SHORT).show();
						}else if(state.equals("1")){
//							TTs();
						}
						orderlist.add(new OrderEntity(id,receive_time,dayno,state, name, phone, address,recmode,paymodeid));
					}
					if(orderlist.size()==0){
						tv_dingdan.setVisibility(View.VISIBLE);
					}else{
						tv_dingdan.setVisibility(View.GONE);
					}
//					Log.i("zq", "isnew---"+isnew);
					
						OrderAdapter adapter = new OrderAdapter(getActivity(), orderlist);
						list_order.setAdapter(adapter);
						list_order.getRefreshableView().setSelection(offset);
						
				} else {
					String msg = response.getString("msg");
					Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
				}
				list_order.onRefreshComplete();
			} catch (JSONException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}

	}
}
